﻿using B.S.Orders.Domain;
using B.S.Orders.ErrorCord;
using B.S.Orders.Write.Api.Application.Command.OrdersCommands;
using MediatR;
using SqlSugar;

namespace B.S.Orders.Write.Api.Application.CommandHander.OrdersCommandsHander
{
    public class DeletedOrdersCommandHander : IRequestHandler<DeletedOrdersCommand, APIResult<object>>
    {
        private readonly ISqlSugarClient db;

        public DeletedOrdersCommandHander(ISqlSugarClient db)
        {
            this.db = db;
        }

        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<APIResult<object>> Handle(DeletedOrdersCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();

            result.Code = APIEnum.success;
            result.Msg = "批量删除成功";

            var ids = request.OrdersId.Split(',').Select(int.Parse).ToList();

            var itemDel = await db.Queryable<OrdersModel>().Where(x => ids.Contains(x.OrdersId)).ToListAsync();

            foreach (var list in itemDel)
            {
                list.Del = true;
                await db.Updateable(list).ExecuteCommandAsync();
            }

            return result;
        }
    }
}
